package com.custom.tab;

import java.util.Stack;

import android.app.Activity;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.widget.ViewAnimator;

public class AbstractTabRootManager {

	private Stack<AbstractViewController> viewControllerInterfaces;
	private AbstractTabActivity activity;
	private ViewAnimator viewAnimator;
	private LayoutInflater layoutInflater;

	public AbstractTabRootManager(AbstractTabActivity activity,LayoutInflater layoutInflater, ViewAnimator viewAnimator) {
		try {
			this.activity = activity;
			this.layoutInflater = layoutInflater;
			this.viewAnimator = viewAnimator;
			this.viewControllerInterfaces = new Stack<AbstractViewController>();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onResume() {
		try {
			if(viewControllerInterfaces.size()>0){
				viewControllerInterfaces.peek().onResume();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void pushViewController(AbstractViewController viewControllerInterface){
		try {
			viewControllerInterfaces.push(viewControllerInterface);
			viewAnimator.addView(viewControllerInterface.getView());
			viewAnimator.setDisplayedChild(viewAnimator.getChildCount()-1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void pushViewControllerClearingAll(AbstractViewController viewControllerInterface){
		try {
			viewAnimator.removeAllViews();
			if(viewControllerInterfaces.size()>0){
				AbstractViewController vci = viewControllerInterfaces.pop();
				vci.onDestroy();
			}
			viewControllerInterfaces.clear();
			pushViewController(viewControllerInterface);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void popViewController(){
		try {
			if(viewControllerInterfaces.size()>0){
				AbstractViewController viewControllerInterface = viewControllerInterfaces.pop();
				if(viewControllerInterfaces.size()>0){
					viewAnimator.setDisplayedChild(viewAnimator.getChildCount()-2);
					viewControllerInterfaces.peek().onResume();
				}
				viewAnimator.removeView(viewControllerInterface.getView());
				viewControllerInterface.onDestroy();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public LayoutInflater getLayoutInflater(){
		return layoutInflater;
	}
	public AbstractTabActivity getActivity(){
		return activity;
	}
	public SharedPreferences getSharedPreferences(){
		return activity.getSharedPreferences();
	}
	public void sendBroadcastMessage(String key,String message){
		 activity.sendBroadcastMessage(key, message);
	}
	public boolean onBackPressed() {
		boolean ret = false;
		try {
			if(viewControllerInterfaces.size()>0){
				ret = viewControllerInterfaces.peek().onBackPressed();
				if(!ret){
					popViewController();
					ret = (viewControllerInterfaces.size()!=0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	public int getChildCount(){
		int ret = 0;
		try {
			ret = viewControllerInterfaces.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	public void onDestroy() {
		try {
			if(viewControllerInterfaces.size()>0){
				AbstractViewController vci = viewControllerInterfaces.pop();
				viewAnimator.removeAllViews();
				vci.onDestroy();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void broadcastMessageReceived(String key, String message){
		try {
			for(int i=0;i<viewControllerInterfaces.size();i++){
				viewControllerInterfaces.get(i).broadcastMessageReceived(key, message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
		try {
			for(int i=0;i<viewControllerInterfaces.size();i++){
				viewControllerInterfaces.get(i).onSharedPreferenceChanged(sharedPreferences, key);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}